package com.my.utils;

import jdk.internal.dynalink.beans.StaticClass;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * package:
 * 工具类---进行拷贝，拷贝数据到另一个类
 * author:
 * date:
 */
public class BeanCopyUtils {
    private BeanCopyUtils(){

    }
    /*静态方法：bean拷贝*/
    /*这里用了泛型*/
    public static <V> V copyBean(Object source,Class<V> clazz){
        //泛型
        V result = null;
        //通过反射创建目标对象，实现属性拷贝，返回结果
        try {
            result = clazz.newInstance();
            //bean拷贝
            BeanUtils.copyProperties(source,result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
//使用了stream流，进行list拷贝
    public static <O,V> List<V> copyBeanList(List<O> list,Class<V> clazz){
        return list.stream()
                .map(o -> copyBean(o,clazz))
                .collect(Collectors.toList());
    }
}
